/*
*********************************
	SF2D Engine
*********************************

	By Julian Williams
	June 11, 2010
**********************************

	Description:

		Camera used to scroll
	through the game's envirnment
***********************************
*/
#include <SFML/Graphics.hpp>
#include <assert.h>

#include "app.h"
#include "levels.h"

#include "Camera.h"
#include "Parallax.h"

#include "script_engine.h"



Camera Cam;



Camera::Camera(void)
{
	sf::Vector2f startPos(Map.Get_TileSize().x*32*3, Map.Get_TileSize().y*32*3);

	Set_Camera( startPos );
	
}




//Register class members
bool Camera::Register_With_ScriptEngine()
{
	int nRet = 0;
	
	nRet = aScriptEngine.GetEngine()->RegisterObjectType("Camera", 0, asOBJ_REF | asOBJ_NOHANDLE); assert( nRet >= 0 );

	
	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void set_cameraX( float )", asMETHOD(Camera, Set_CameraX), asCALL_THISCALL); assert( nRet >= 0 );
	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void set_cameraY( float )",  asMETHOD(Camera, Set_CameraY), asCALL_THISCALL); assert( nRet >= 0 );

	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "float get_cameraX( )",  asMETHOD(Camera, Get_CameraX), asCALL_THISCALL); assert( nRet >= 0 );
	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "float get_cameraY( )",  asMETHOD(Camera, Get_CameraY), asCALL_THISCALL); assert( nRet >= 0 );

	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void move_cameraX( float )",  asMETHOD(Camera, Move_CameraX), asCALL_THISCALL); assert( nRet >= 0 );
	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void move_cameraY( float )",  asMETHOD(Camera, Move_CameraY), asCALL_THISCALL); assert( nRet >= 0 );

//	nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void set_scrollX( float )",  asMETHOD(Camera, Set_ScrollX), asCALL_THISCALL); assert( nRet >= 0 );
	//nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "void set_scrollY( float )",  asMETHOD(Camera, Set_ScrollY), asCALL_THISCALL); assert( nRet >= 0 );
	
	//nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "float get_scrollX( )",  asMETHOD(Camera, Get_ScrollX), asCALL_THISCALL); assert( nRet >= 0 );
	//nRet = aScriptEngine.GetEngine()->RegisterObjectMethod("Camera", "float get_scrollY( )",  asMETHOD(Camera, Get_ScrollX), asCALL_THISCALL); assert( nRet >= 0 );
	


	nRet = aScriptEngine.GetEngine()->RegisterGlobalProperty("Camera Cam", &Cam);  assert( nRet >= 0 );
	
	 if(nRet < 0)
	 {
		 cout << "Register Class Result: " << nRet << endl ;
		 return false;
	 }

	return true;
}//EoF




//Setter for Camera Vector
//Grab Camera position;
void Camera::Set_Camera( sf::Vector2f new_position ) 
{	
	//X coordinate
	//Make sure the Camera isn't greater than map size
	Set_CameraX(new_position.x);
	
	//Y Coordinate
	Set_CameraY(new_position.y);

}//EoF



//Set X camera
void Camera::Set_CameraX( float new_x ) 
{


	if(new_x > Map.Get_PixelSize().x - (App::getInstance().Get_RenderWindow()->GetWidth() + (Map.Get_TileSize().x*1)))
	{
		new_x = Map.Get_PixelSize().x - (App::getInstance().Get_RenderWindow()->GetWidth() + (Map.Get_TileSize().x*1));
	}

	//make sure camera isn't less than camera size
	if(new_x < Map.Get_TileSize().x)
	{
		new_x = Map.Get_TileSize().x;
	}

	scroll_pos.x = new_x;
	//Set final camera position
	SetX( new_x );
//	scroll_pos.x = 0;

}//EoF



//Set Y Camera
void Camera::Set_CameraY( float new_y )
{

	
	if(new_y > (Map.Get_PixelSize().y) - (App::getInstance().Get_RenderWindow()->GetHeight() + (Map.Get_TileSize().y*1)))
	{
		new_y = Map.Get_PixelSize().y - (App::getInstance().Get_RenderWindow()->GetHeight() + (Map.Get_TileSize().y*1));
	}

	//make sure camera isn't less than camera size
	if(new_y < Map.Get_TileSize().y)
	{
		new_y = Map.Get_TileSize().y;
	}

	scroll_pos.y = new_y;
	//Set CameraY position
	SetY( new_y );
	//scroll_pos.y = 0;

}//EoF



//Move Camera
void  Camera::Move_Camera(sf::Vector2f new_position)
{
	//sf::Vector2f temp;
	//temp.x = GetPosition().x + new_position.x;
	//temp.y = GetPosition().y + new_position.y;

	//Set_Camera(temp);

	Move_CameraX( new_position.x );
	Move_CameraY( new_position.y );
}



void  Camera::Move_CameraX(float new_x)
{
	new_x *= App::getInstance().Get_RenderWindow()->GetFrameTime();

	

	Set_CameraX(GetPosition().x + new_x);
	//for parallax Background
	scroll_pos.x = new_x;
	//cout << scroll_pos.x << endl;

	//printf("scroll_pos.x: %f\n", scroll_pos.x);
	//Parallax::Set_ScrollX( new_x );
}



void  Camera::Move_CameraY(float new_y)
{
	new_y *= App::getInstance().Get_RenderWindow()->GetFrameTime();

	Set_CameraY(GetPosition().y + new_y);
	//for parallax background
	scroll_pos.y = new_y;
	//cout << scroll_pos.y << endl;

	//Parallax::Set_ScrollY( new_y );

}



//Check to see if we can scroll
bool  Camera::canScroll_Left()
{
	if(GetPosition().x > Map.Get_TileSize().x)
	{
		return true;
	}

	return false;
}//EoF

bool  Camera::canScroll_Right()
{
	if(GetPosition().x < Map.Get_PixelSize().x - (App::getInstance().Get_RenderWindow()->GetWidth() + Map.Get_TileSize().x))
	{
		return true;
	}
	
	return false;
}//EoF

bool  Camera::canScroll_Up()
{
	if(GetPosition().y > Map.Get_TileSize().y)
	{
		return true;
	}

	return false;
}//EoF

bool  Camera::canScroll_Down()
{
	if(GetPosition().y < Map.Get_PixelSize().y - (App::getInstance().Get_RenderWindow()->GetHeight() + Map.Get_TileSize().y))
	{
		return true;
	}

	return false;
}//EoF